package com.ttg.web.service.Impl;
import java.time.LocalDate;


import com.ttg.common.constant.BasicConstant;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.enums.ResultStatusEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.model.dto.RattingRulesListAddDto;
import com.ttg.model.dto.RattingRulesListAddInfoDto;
import com.ttg.model.dto.RattingRulesListDto;
import com.ttg.model.dto.open.RattingRulesDeleteDto;
import com.ttg.model.pojo.RulesList;
import com.ttg.model.pojo.VipRatingConfig;
import com.ttg.model.vo.RattingRulesListAddtVo;
import com.ttg.model.vo.RattingRulesListInfoVo;
import com.ttg.model.vo.RattingRulesListVo;
import com.ttg.model.vo.ResultStatusVo;
import com.ttg.web.dao.RulesListDao;
import com.ttg.web.dao.VipRattingConfigDao;
import com.ttg.web.service.VipRattingConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangHongYuan
 * @description
 * @date 2023/6/8 11:19
 */
@Service
public class VipRattingConfigServiceImpl implements VipRattingConfigService {
    @Autowired
    private VipRattingConfigDao vipGradeRattingDao;

    @Autowired
    private RulesListDao rulesListDao;

    @Override
    public List allList() {
        return vipGradeRattingDao.list();
    }

    @Override
    public RattingRulesListVo getRattingRulesListByRulesId(RattingRulesListDto dto) {
        if (Objects.isNull(dto) || StringUtils.isBlank(dto.getRulesId())){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        RulesList byId = rulesListDao.getById(dto.getRulesId());
        if (Objects.isNull(byId)){
            throw new BaseException(ErrorCodeEnums.TASK_RULES_IS_NOT_EXIST);
        }
        RattingRulesListVo rattingRulesListVo = new RattingRulesListVo();
//        rattingRulesListVo.setGrowthMax(String.valueOf(byId.getGrowthMax()) );
        rattingRulesListVo.setSuffix(String.valueOf(byId.getSuffix()));
        List<VipRatingConfig> rattingRulesListByRulesId = vipGradeRattingDao.getRattingRulesListByRulesId(dto.getRulesId());
        List<RattingRulesListInfoVo> list = new ArrayList<>();
        rattingRulesListByRulesId.forEach(r->{
            RattingRulesListInfoVo vo = new RattingRulesListInfoVo();
            vo.setRatingStart(String.valueOf( r.getRatingStart()));
            vo.setRatingEnd(String.valueOf(r.getRatingEnd()));
            vo.setScore(String.valueOf(r.getScore()));
            vo.setId(String.valueOf(r.getId()));
            if (Objects.isNull(r.getContinuousMonth())){
                r.setContinuousMonth(0L);
            }
            vo.setContinuousMonth(String.valueOf(r.getContinuousMonth()));
            list.add(vo);
        });
        rattingRulesListVo.setListInfoVo(list);
        return rattingRulesListVo;
    }

    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    @Override
    @Transactional
    public ResultStatusVo addRattingList(RattingRulesListAddDto dto) {
        if (Objects.isNull(dto) ||StringUtils.isBlank(dto.getRulesId())|| CollectionUtils.isEmpty(dto.getListInfoVo())){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }

//        if (StringUtils.isBlank(dto.getGrowthMax())||BasicConstant.LONG_0>Long.parseLong(dto.getGrowthMax())){
//            throw new BaseException(ErrorCodeEnums.RULES_GROWTH_MAX_ERROR);
//        }

        RulesList byId = rulesListDao.getById(dto.getRulesId());
        if (Objects.isNull(byId)){
            throw new BaseException(ErrorCodeEnums.TASK_RULES_IS_NOT_EXIST);
        }

        List<RattingRulesListAddInfoDto> listInfoVo = dto.getListInfoVo();
        if (listInfoVo.size()>BasicConstant.INT_10){
            throw new BaseException(ErrorCodeEnums.RULES_NUMBER_IS_MAX);
        }
        List<VipRatingConfig> list = new ArrayList<>();
        VipRatingConfig orderRattingConfig=null;
        int i = 1;
        for (RattingRulesListAddInfoDto r : listInfoVo) {
            if (Objects.isNull(r.getRatingStart())||Objects.isNull(r.getRatingEnd())){
                throw new BaseException(ErrorCodeEnums.RULES_CONDITIONS_IS_NULL);
            }

            if (BasicConstant.INT_0>r.getRatingStart()
                    ||  BasicConstant.INT_0>r.getRatingEnd()
                    ){
                throw new BaseException(ErrorCodeEnums.RULES_CONDITIONS_IS_ERROR);

            }

            if (r.getRatingStart()>=r.getRatingEnd()&&i!=listInfoVo.size()){
                throw new BaseException(ErrorCodeEnums.RULES_CONDITIONS_IS_ERROR);
            }
            if (i==listInfoVo.size()&&r.getRatingEnd()!=BasicConstant.INT_0&&r.getRatingEnd()<=r.getRatingStart()){
                throw new BaseException(ErrorCodeEnums.RULES_CONDITIONS_IS_ERROR);
            }

            if (Objects.isNull(orderRattingConfig)){
                r.setRatingStart(BasicConstant.LONG_0);
            }else {
              if (!orderRattingConfig.getRatingEnd().equals(r.getRatingStart())){
                  throw new BaseException(ErrorCodeEnums.RULES_CONDITIONS_START_IS_ERROR);
              }
            }
            if (Objects.isNull(r.getScore())){
                r.setScore(BasicConstant.LONG_0);
            }
            if (Objects.isNull(r.getContinuousMonth())){
                r.setContinuousMonth(BasicConstant.INT_0);
            }

            VipRatingConfig ratingConfig = new VipRatingConfig();
            if (StringUtils.isNotBlank(r.getId())){
                ratingConfig.setId(Integer.valueOf(r.getId()));
            }else {
                ratingConfig.setSettingTime(LocalDate.now());
            }
            ratingConfig.setRatingStart(r.getRatingStart());
            ratingConfig.setRatingEnd(r.getRatingEnd());
            ratingConfig.setScore(r.getScore());
            ratingConfig.setRulesId(Long.valueOf(dto.getRulesId()));
            ratingConfig.setContinuousMonth(Long.valueOf(r.getContinuousMonth()));

            list.add(ratingConfig);
            orderRattingConfig= ratingConfig;
            i++;
        }


        byId.setUpdateTime(LocalDateTime.now());
//        byId.setGrowthMax(Long.valueOf(dto.getGrowthMax()));
        rulesListDao.updateById(byId);
        vipGradeRattingDao.saveOrUpdateBatch(list);
        return new ResultStatusVo(ResultStatusEnum.SUCCESS.getCode());
    }

    @Override
    public ResultStatusVo deleteRattingList(RattingRulesDeleteDto dto) {
        if (Objects.isNull(dto) ||CollectionUtils.isEmpty(dto.getIds())){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        dto.getIds().forEach(id->{
            VipRatingConfig byId = vipGradeRattingDao.getById(id);
            if (Objects.isNull(byId)){
                throw new BaseException(ErrorCodeEnums.TASK_RULES_IS_NOT_EXIST);
            }
        });

        boolean b = vipGradeRattingDao.removeByIds(dto.getIds());
        if (b){
            return new ResultStatusVo(ResultStatusEnum.SUCCESS.getCode());
        }
        return new ResultStatusVo(ResultStatusEnum.FAIL.getCode());
    }
}
